package com.cq.school.web;

import com.cq.school.config.Config;
import com.cq.school.entity.Apply;
import com.cq.school.entity.Article;
import com.cq.school.service.ApplyService;
import com.cq.school.service.ArticleService;
import com.cq.school.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springside.modules.web.Servlets;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User:oa3158
 * Date: 14-1-7
 * Time: 上午9:18
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class SchoolAction {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private ApplyService applyService;
    @Autowired
    private Config config;

    @RequestMapping(value = "/index")
    public String index(Model model){
        model.addAttribute("recomm1",schoolService.listRecommenSchool(1,4));
        model.addAttribute("recomm2",schoolService.listRecommenSchool(2,4));
        model.addAttribute("articles",articleService.listArticleByCreateTime(8));
        model.addAttribute("articleCategory",config.getArticleCategory());
        model.addAttribute("categorys",config.getArticleCategory());
        model.addAttribute("apply",new Apply());
        model.addAttribute("schools",schoolService.listSchool());
        return "site/index";
    }
    @RequestMapping(value = "/apply",method = RequestMethod.GET)
    public String apply(Model model){
        model.addAttribute("apply",new Apply());
        model.addAttribute("schools",schoolService.listSchool());
     return "site/apply";
    }

    @RequestMapping(value = "/apply",method = RequestMethod.POST)
    public String apply(Apply apply){
        applyService.saveApply(apply);
        return "redirect:/index";
    }
    @RequestMapping(value = "/article/list",method = RequestMethod.GET)
    public String listArticle(@RequestParam(value = "page", defaultValue = "1") int pageNumber,
                              @RequestParam(value = "page.size", defaultValue = "10") int pageSize,
                              @RequestParam(value = "cid", defaultValue = "0") int category,
                              Model model, ServletRequest request) {
        Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");

        Page<Article> list = articleService.listArticle(category,searchParams,pageNumber,pageSize);
        model.addAttribute("categorys",config.getArticleCategory());
        model.addAttribute("list", list);
        model.addAttribute("cid",category);
        if(category>0)
            model.addAttribute("categoryName",config.getArticleCategory().get(category));
        // 将搜索条件编码成字符串，用于排序，分页的URL
        model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));
        return "site/article_list";
    }

    @RequestMapping(value = "/getSpecialtys",method = RequestMethod.GET)
    @ResponseBody
    public Object getSpecialtys(@RequestParam("schoolId") long schoolId){
        return schoolService.findSpecialtyBySchoolId(schoolId);
    }

    @RequestMapping(value = "/school/detail/{id}")
    public String schoolDetail(@PathVariable("id") Long id,Model model){
        model.addAttribute("school",schoolService.getSchool(id));
        model.addAttribute("categorys",config.getArticleCategory());
        model.addAttribute("picList",schoolService.listPictures(id));
        model.addAttribute("articleList",articleService.listSchoolArticle(id,6));
        return "site/school";
    }

    @RequestMapping(value = "/article/view/{id}")
    public String viewArticle(@PathVariable("id") Long id,Model model){
        Article article = articleService.getArticle(id);
        articleService.updateHits(id);
        model.addAttribute("categorys",config.getArticleCategory());
        model.addAttribute("article",article);
        model.addAttribute("category",config.getArticleCategory().get(article.getCategory()));
        List<Article> list = articleService.getNexPre(id);
        for(Article art:list)
        {
            if (art.getId()>id)
                model.addAttribute("next",art);
            if(art.getId()<id)
                model.addAttribute("pre",art);
        }
        return "site/article";
    }

}
